package com.xzg.erp.product.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xzg.erp.product.entity.ProductType;
import com.xzg.erp.product.entity.ProductTypeProduct;
import com.xzg.erp.product.mapper.ProductMapper;
import com.xzg.erp.product.mapper.ProductTypeMapper;
import com.xzg.erp.product.service.IProductTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 产品类型表 服务实现类
 * </p>
 *
 * @author qwc
 * @since 2025-02-12
 */
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements IProductTypeService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public void logicDelete(List<Integer> ids) {
        LocalDateTime now = LocalDateTime.now();
        int userId = StpUtil.getLoginIdAsInt();
        List<ProductType> list = this.list(Wrappers.<ProductType>lambdaQuery().in(ProductType::getId, ids));
        list.forEach(type->{
            type.setDeleted(type.getId());
            type.setUpdatedAt(now);
            type.setUpdatedBy(userId);
        });
        this.updateBatchById(list);
        deleteTags(ids, userId, now);
    }

    private void deleteTags(List<Integer> ids, int userId, LocalDateTime now) {
        List<ProductTypeProduct> pTypes = this.getBaseMapper().selectTypesByProductId(ids);
        pTypes.forEach(type -> {
            type.setDeleted(type.getId());
            type.setUpdatedBy(userId);
            type.setUpdatedAt(now);
        });
        productMapper.deleteTypesIds(pTypes);
    }
}
